package com.tuzhi.base.jpa.domain;

import com.tuzhi.base.jpa.enums.AndOrOperate;
import com.tuzhi.base.jpa.enums.QueryTypeEnum;
import com.tuzhi.base.jpa.enums.SortOrderEnum;

import java.util.ArrayList;
import java.util.List;

public class WhereConditionBO {

    List<QueryConditionBO> queryList;

    List<SortConditionBO> sortList;

    public WhereConditionBO() {
        queryList = new ArrayList<>();
        sortList = new ArrayList<>();
    }

    public WhereConditionBO(List<QueryConditionBO> queryList, List<SortConditionBO> sortList) {
        this();
        if (queryList != null) {
            this.queryList = queryList;
        }
        if (sortList != null) {
            this.sortList = sortList;
        }
    }

    public WhereConditionBO addQueryConditionBO
            (String propertyName, Object valObject) {
        this.queryList.add(new QueryConditionBO(propertyName, valObject, QueryTypeEnum.equal, null, AndOrOperate.and));
        return this;
    }

    public WhereConditionBO addQueryConditionBO
            (String propertyName, Object valObject, QueryTypeEnum queryTypeEnum, AndOrOperate andOrOperate) {
        this.queryList.add(new QueryConditionBO(propertyName, valObject, queryTypeEnum, null, andOrOperate));
        return this;
    }

    public WhereConditionBO addQueryConditionBO4List
            (String propertyName, List valObjects, QueryTypeEnum queryTypeEnum, AndOrOperate andOrOperate) {
        this.queryList.add(new QueryConditionBO(propertyName, null, queryTypeEnum, valObjects, andOrOperate));
        return this;
    }

    public WhereConditionBO addSortConditionBO(String propertyName, String sortOrder) {
        for (SortConditionBO sortConditionBO : this.sortList) {
            if (sortConditionBO.getPropertyName().equals(propertyName)) {
                //重复排序字段，返回
                return this;
            }
        }
        this.sortList.add(new SortConditionBO(propertyName, sortOrder));
        return this;
    }

    public WhereConditionBO addSortConditionBO(String propertyName, SortOrderEnum sortOrderEnum) {
        return addSortConditionBO(propertyName, sortOrderEnum.name());
    }

    public List<QueryConditionBO> getQueryList() {
        return queryList;
    }

    public void setQueryList(List<QueryConditionBO> queryList) {
        this.queryList = queryList;
    }

    public List<SortConditionBO> getSortList() {
        return sortList;
    }

    public void setSortList(List<SortConditionBO> sortList) {
        this.sortList = sortList;
    }
}

